package com.doumuxie.linstener;

import com.doumuxie.frame.ChildFrame;
import com.doumuxie.frame.MainFrame;
import com.doumuxie.frame.ProductConsumeFrame;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

/**
 * @author doumuxie https://gitee.com/doumuxie/
 * @version 1.0
 * @date 2020/9/26 9:00
 * @description 主窗口事件
 **/
public class MainListener implements ActionListener {

    private static File file;

    private MainFrame myFrame;

    public MainListener(MainFrame frame) {
        this.myFrame = frame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == myFrame.getJtbOpen() || source == myFrame.getMiOpen()) {
            // 打开事件
            try {
                JFileChooser jFileChooser = new JFileChooser();
                jFileChooser.showDialog(myFrame, "打开");
                file = jFileChooser.getSelectedFile();
                if (file == null) {
                    return;
                }
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String line;
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                    sb.append("\r\n");
                }
                reader.close();
                myFrame.getTextArea().setText(sb.toString());

            } catch (Exception e2) {
                e2.printStackTrace();
                System.err.println("文件打开异常");
            }
            System.out.println(file);
        } else if (source == myFrame.getMiExit() || source == myFrame.getJtbExit()) {
            // 退出事件
            int judge = JOptionPane.showConfirmDialog(myFrame, "确认退出？");
            if (judge == JOptionPane.OK_OPTION) {
                System.exit(0);
            }
        } else if (source == myFrame.getMiSave() || source == myFrame.getJtbSave()) {
            // 保存事件
            System.out.println("====");
            System.out.println(file);
            if (file == null) {
                final JFileChooser SaveAs = new JFileChooser();
                SaveAs.setApproveButtonText("Save");
                SaveAs.showOpenDialog(myFrame);
                file = new File(SaveAs.getSelectedFile() + ".txt");
            }
            try {
                BufferedWriter outFile = new BufferedWriter(new FileWriter(file));
                outFile.write(myFrame.getTextArea().getText().replaceAll("\n", "\r\n"));
                outFile.close();
            } catch (IOException ex) {
            }
        } else if (source == myFrame.getpChange()) {
            // 打开子窗口
            ChildFrame childFrame = new ChildFrame();
            childFrame.setVisible(true);
        } else if (source == myFrame.getEditAllow()) {
            // 允许编辑
            myFrame.getTextArea().setEnabled(true);
        } else if (source == myFrame.getEditDeny()) {
            // 禁止编辑
            myFrame.getTextArea().setEnabled(false);
        }else if (source == myFrame.getpConsume()){
            // 打开子窗口
            ProductConsumeFrame childFrame = new ProductConsumeFrame();
            childFrame.setVisible(true);
        }

    }

}
